package com.hro.hro_app.main;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hro.hro_app.R;
import com.hro.hro_app.dataController.LessonController;
import com.hro.hro_app.dataController.TeacherController;
import com.hro.hro_app.model.Lesson;

@SuppressLint("SimpleDateFormat")
public class DayRooster extends Activity implements RoosterDataChanged {

	public static boolean isOnFront;
	public static DayRooster dayRooster;

	@Override
	protected void onResume() {

		DayRooster.isOnFront = true;
		DayRooster.dayRooster = this;
		super.onResume();
	}

	@Override
	protected void onPause() {

		DayRooster.isOnFront = false;
		super.onPause();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_day_rooster);

		DayRooster.dayRooster = this;

		showLessons();
	}

	public void showLessons() {

		ArrayList<Lesson> list = new ArrayList<Lesson>();
		list = LessonController.getLessonsByDay(LessonController.getNextLesDay());

		LinearLayout ll = (LinearLayout) findViewById(R.id.linLayout);
		ll.removeAllViews();

		for (int i = 0; i < list.size(); i++) {

			Lesson lesson = list.get(i);
			TextView tv = new TextView(this);
			SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
			SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM");

			String date = sdfDate.format(lesson.getStart());
			String end = sdfTime.format(lesson.getEnd());
			String from = sdfTime.format(lesson.getStart());

			String lessonstring;
			lessonstring = lesson.getModule() + '\n' + lesson.getTeacher() + " " + lesson.getLocation() + '\n' + date + " " + from + "-" + end;

			// Controleer of teacher aanwezig is
			if (TeacherController.getTeacherAbsent(lesson.getTeacher())) {
				tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			}

			// Begin layout
			tv.setWidth(390);
			tv.setTextSize(18);
			tv.setTextColor(Color.WHITE);
			tv.setBackgroundColor(Color.argb(255, 204, 0, 51));
			tv.setPadding(15, 15, 15, 15); // (left, top, right, bottom);

			LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			llp.setMargins(0, 0, 0, 20); // llp.setMargins(left, top, right, bottom);
			tv.setLayoutParams(llp);
			// Einde laypout

			tv.setText(lessonstring);
			ll.addView(tv);
		}

		ll.invalidate();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.day_rooster, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

			case R.id.action_les_view:
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				return true;
			case R.id.action_refresh:
				LessonController.refreshData(this);
				return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void roosterDataChanged() {

		showLessons();
	}
}
